﻿var myClick = false;
var new_TemplateID = TemplateID;
function actionActivity(aid,item,TemplateID) {
    $.ajax({
        type: "get",
        url: '/app/PreView/GetAwards',
        data: { "aId": aid },
        async: false,
        success: function (data) {
            var YOrNAward = data.Denomination;
            if (data.Denomination == -1) { //没有中到奖品
                // 跳转到没中奖页面
                JGG(YOrNAward, 0, TemplateID);
                return TemplateID;
            }
            else {  //中到奖品了
                JGG(YOrNAward, data.Sequence);
                $('.bg-con').children('.small-bg').children('a.add').addClass('animated  zoomIn');
                if (data.type == 3) { //流量
                    $('#awardImg').attr("src", data.AwardsIosUrl)
                    $(".bg-con .small-bg p:nth-child(2) span").text('价值' + data.Denomination + '元流量券').css({ 'width': 'auto', 'padding': '0 5px', 'color': '#E23524' });
                    $(".bg-con .small-bg p:nth-child(3) span").text(data.Name);
                    $('.bg-con .add').attr('href', 'Verification?awId=' + data.Id + '&Aid=' + data.ActivityId + '&Denomination=' + data.Denomination);
                }
                else if (data.type == 2) {//优惠券
                }
                else { //红包
                    $('#awardImg').attr("src", data.AwardsIosUrl)
                    $(".bg-con .small-bg p:nth-child(2) span").text(data.Denomination + '元').css({ 'width': 'auto', 'padding': '0 10px', 'color': '#E23524' });
                    $(".bg-con .small-bg p:nth-child(3) span").text(data.Name);
                    $('.bg-con .add').attr('href', 'Verification?awId=' + data.Id + '&Aid=' + data.ActivityId + '&Denomination=' + data.Denomination);
                }
            }
        }
    });
    var TemplateID;
    function JGG(YOrNAward, Sequence, TemplateID) {
        debugger
        var lottery = {
            index: -1, //当前转动到哪个位置，起点位置
            count: 0, //总共有多少个位置
            timer: 0, //setTimeout的ID，用clearTimeout清除
            speed: 20, //初始转动速度
            times: 0, //转动次数
            cycle: 50, //转动基本次数：即至少需要转动多少次再进入抽奖环节
            prize: -1, //中奖位置
            init: function (id) {
                if ($('#' + id).find('.lottery-unit').length > 0) {
                    $lottery = $('#' + id);
                    $units = $lottery.find('.lottery-unit');
                    this.obj = $lottery;
                    this.count = $units.length;
                    $lottery.find('.lottery-unit.lottery-unit-' + this.index).addClass('active');
                };
            },
            roll: function (TemplateID) {
                var index = this.index;
                var count = this.count;
                var lottery = this.obj;
                $(lottery).find('.lottery-unit.lottery-unit-' + index).removeClass('active');
                index += 1;
                if (index > count - 1) {
                    index = 0;
                };
                $(lottery).find('.lottery-unit.lottery-unit-' + index).addClass('active');
                this.index = index;
                return false;
            },
            stop: function (index) {
                this.prize = index;
                return false;
            }
        };
        
        function roll(TemplateID) {
            lottery.times += 1;
            lottery.roll(); //转动过程调用的是lottery的roll方法，这里是第一次调用初始化
            if (lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index) {
                clearTimeout(lottery.timer);

                setTimeout(function () {

                    //是否中奖弹出框  停顿一秒
                    if (YOrNAward == -1) {
                        GetError(new_TemplateID);
                    }
                    else {
                        $('#bg-con>div:nth-child(2)').addClass("receive");
                        $('#bg-con').css('display', 'flex');

                    }

                }, 1000);

                lottery.prize = -1;
                lottery.times = 0;
                click = false;
            } else {
                if (lottery.times < lottery.cycle) {
                    lottery.speed -= 10;
                } else if (lottery.times == lottery.cycle) {

                    //中奖位置
                    var array = [];
                    var img = "img" + Sequence;
                    console.log($("." + img))
                    $("." + img).each(function (index, ele) {
                        array.push($(this).attr('id').slice(3));
                    })
                    var index = Math.floor((Math.random() * array.length));
                    lottery.prize = array[index];
                    console.log(array)
                } else {
                    if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) {
                        lottery.speed += 110;
                    } else {
                        lottery.speed += 20;
                    }
                }
                if (lottery.speed < 40) {
                    lottery.speed = 40;
                };
                lottery.timer = setTimeout(roll, lottery.speed); //循环调用
            }
            return false;
        }
        lottery.init('lottery');

        if (myClick) { //click控制一次抽奖过程中不能重复点击抽奖按钮，后面的点击不响应
            return false;
        } else {
            lottery.speed = 100;
            roll(TemplateID); //转圈过程不响应click事件，会将click置为false
            myClick = true; //一次抽奖完成后，设置click为true，可继续抽奖
            return false;
        }
    }
}